package card.dealing;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class CardDemo {
    public static final String[] suits = {"♥","♣","♦","♠"};

    //产生一副牌
    public List<Card> BuyCard(){
        List<Card> cards = new ArrayList<>();

        for (int i = 0; i < 13; i++) {
            for (int j = 0; j < 4; j++) {
                String suit = suits[j];//获取花色
                int rank = j + 1;//牌的数字
                Card card = new Card(suit,rank);
                cards.add(card);
            }
        }

        return cards;
    }


    //打乱一副牌
    public List<Card> MixCard(List<Card> cards){
        Random random = new Random();
        for (int i = cards.size()-1; i > 0; i--) {
            int index = random.nextInt(i);
            swap(cards, index, i);
        }
        return cards;
    }

    private void swap(List<Card> cards,int j,int i){
        Card tmp = cards.get(j);
        cards.set(j,cards.get(i));
        cards.set(i,tmp);
    }



    //发配：三个人，依次发五张
    public List<List<Card>>  play(List<Card> cards){
        //三个人
        List<Card> hand0 = new ArrayList<>();
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();

        List<List<Card>> ret = new ArrayList<>();
        ret.add(hand0);
        ret.add(hand1);
        ret.add(hand2);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                Card card = cards.remove(0);//从牌堆顶拿牌
                ret.get(j).add(card);
            }
        }


        return ret;
    }
}
